package com.bxait.toymail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.R;
import com.bxait.com.vo.product.AttrRespVo;
import com.bxait.com.vo.product.AttrVo;
import com.bxait.toymail.product.entity.ProductAttrValueEntity;
import com.bxait.toymail.product.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.bxait.toymail.product.service.AttrService;

import javax.annotation.Resource;


/**
 * import org.apache.shiro.authz.annotation.RequiresPermissions;
 * 商品属性:指的就是就这个spu的商品属性
 * @author chen
 * @email 2277411582@qq.com
 * @date 2023-03-13 22:26:35
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Resource
    @Qualifier("attrService")
    private AttrService attrService;

    @Resource
    @Qualifier("productAttrValueService")
    ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * @param
     * @==============History===============<br/>
     * @Description // TODO  按照属性的类型,以及所在的三级分类进行分页获取属性信息
     * @Date 2023/3/18 2023/3/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return []
     */
    @GetMapping("/{attrType}/list/{cateLogId}")
    public R attrTypeListByCateLogId(@RequestParam Map<String, Object> params,
                                     @PathVariable("cateLogId") Long cateLogId,
                                     @PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryBaseAttrPage(params,cateLogId,attrType);

        return R.ok().put("data", page);
    }



    /**
     * @param spuId
     * @==============History===============<br/>
     * @Description // TODO  获取当前商品(spu)的规格参数
     * @Date 2023/3/14 22:39
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.com.utils.R
     */
    @GetMapping("/base/listForSpu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data", entities);
    }




    /**
     * @param attrId
     * @==============History===============<br/>
     * @Description // TODO  获取属性的详情信息
     * @Date 2023/3/18 2023/3/18 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [java.lang.Long]
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("data", attr);
    }

    /**
     * @param attr
     * @==============History===============<br/>
     * @Description // TODO  保存属性信息,以及属性分组信息
     * @Date 2023/3/18 2023/3/18 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [com.bxait.toymail.product.entity.AttrEntity]
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * @param attr
     * @==============History===============<br/>
     * @Description // TODO  修改商品属性信息
     * @Date 2023/3/18 2023/3/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [com.bxait.com.vo.product.AttrVo]
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo  attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    /**
     * @param spuId
     * @param entities
     * @==============History===============<br/>
     * @Description // TODO  修改商品规格
     * @Date 2023/3/21 20:04
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.com.utils.R
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

}
